app.factory('newsReader', ['dataRequest', function newsReaderFactory(dataRequest) {
  var cachedNews;

  function read(count) {
    var def = jQuery.Deferred();

    if (cachedNews) {
      def.resolve(cachedNews.slice(0, count));
    }

    // If no cache, get news, if cache refresh it anyway.
    dataRequest.read('News', null, count || null).then(function(data) {
        cachedNews = data.results;
        def.resolve(cachedNews);
    }, function(err) {
      def.reject(err);
    });

    return def.promise();
  }

  return {
    read: read
  };
}]);